package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.common.constants.user.AdminConstants;
import com.usian.common.dtas.BaseServiceImpl;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.LinkedHashMap;

@Service
public class ApUserRealnameServiceImpl extends BaseServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    @Autowired
    private ApAuthorFeign apAuthorFeign;
    @Autowired
    private WemediaFeign wemediaFeign;
    @Autowired
    private ApUserMapper apUserMapper;

    @Override
    public PageResponseResult loadListByStatus(AuthDto dto) {
        //1.检查参数
        if (dto == null || dto.getStatus() == null) return (PageResponseResult) ErrorParam;
        if (dto.getStatus() != null) {
            LambdaQueryWrapper<ApUserRealname> wrapper = new QueryWrapper<ApUserRealname>().lambda().eq(ApUserRealname::getStatus, dto.getStatus());
            Page page = new Page(dto.getPage(), dto.getSize());
            page(page, wrapper);
            PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
            responseResult.setData(page.getRecords());
            return responseResult;
        }
        return (PageResponseResult) ErrorParam;
    }

    @Override
    @GlobalTransactional
    public ResponseResult updateStatusById(AuthDto dto, Short status) {
        //1.检查参数
        if (dto.getId() == null || dto == null) return ErrorParam;
        ApUserRealname realname = getById(dto.getId());
        //根据认证id查询用户表的信息
        ApUser apUser = apUserMapper.selectById(realname.getUserId());
        //2.修改认证表的状态
        ApUserRealname userRealname = new ApUserRealname();
        userRealname.setId(dto.getId());
        userRealname.setStatus(status);
        if (status == AdminConstants.PASS_AUTH) {
            updateById(userRealname);
        }
        if (dto.getMsg() != null) {
            userRealname.setReason(dto.getMsg());
        }
        //根据认证表的名称查找用户信息/
        WmUser byName = wemediaFeign.findByName(apUser.getName());
        if (byName == null) {
            //3.修改用户认证表的状态之后同步用户自媒体表
            WmUser wmUser = new WmUser();
            //获取到认证id
            wmUser.setApUserId(apUser.getId());
            BeanUtils.copyProperties(apUser, wmUser);
            wmUser.setCreatedTime(new Date());
            wmUser.setStatus(9);
            ResponseResult result = wemediaFeign.save(wmUser);


            LinkedHashMap data = (LinkedHashMap) result.getData();
            Integer id = (Integer) data.get("id");

            ApAuthor byUserId = apAuthorFeign.findByUserId(apUser.getId());
            if (byUserId == null) {
                ApAuthor apAuthor = new ApAuthor();
                apAuthor.setUserId(apUser.getId());
                apAuthor.setCreatedTime(new Date());
                apAuthor.setName(apUser.getName());
                apAuthor.setWmUserId(id);
                apAuthor.setType(2);
                ResponseResult save = apAuthorFeign.save(apAuthor);

                LinkedHashMap data1 = (LinkedHashMap) save.getData();
                Integer id1 = (Integer) data1.get("id");
                WmUser byName1 = wemediaFeign.findByName(apAuthor.getName());
                byName1.setApAuthorId(id1);
                wemediaFeign.updateAuthor(byName1);

            }else {
                return DatA;
            }
            //4.修改用户的信息qq
            ApUser apUser1 = new ApUser();
            apUser1.setId(apUser.getId());
            apUser1.setFlag((short) 1);
            apUserMapper.updateById(apUser1);

        }
        int i=1/0;
        return Success;
    }

}
